package structure.myStack;

import java.util.Arrays;
import java.util.Stack;

public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack(){
        this.elem = new int[5];
    }

    public void push(int val){
        if (isFull()){
            //二倍扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = val;
        this.usedSize++;
    }

    public boolean isFull(){
        return this.usedSize == this.elem.length;
    }

    public int pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空!");
        }
        int oldVal = this.elem[usedSize-1];
        this.usedSize--; //因为这里放的是简单类型,如果放的是引用类型还要多一步,先把elem[usedSize-1] = null 之后再进行这一步.
        return oldVal;
    }

    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("栈为空!");
        }
        return this.elem[usedSize-1];
    }

    public boolean isEmpty(){
        return this.usedSize == 0;
    }
}


